﻿//The MIT License

//Copyright (c) 2009 Lluis Sanchez, Nick Darnell

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace Polymorph
{
    /// <summary>
    /// The <see cref="CodeNot"/> class represents the unary operator '!' used to perform
    /// a logical negation of a boolean expression.
    /// </summary>
    public class CodeNot : CodeConditionExpression
    {
        private CodeExpression m_exp;

        /// <summary>
        /// Initializes a new instance of <see cref="CodeNot"/> with the boolean code 
        /// expression to negate.
        /// </summary>
        /// <param name="exp">The boolean expression to negate.</param>
        /// <exception cref="InvalidOperationException">If the exp does not have a boolean 
        /// result type.</exception>
        public CodeNot(CodeExpression exp)
        {
            m_exp = exp;

            if (exp.ResultType != typeof(bool))
            {
                throw new InvalidOperationException(
                    "Operator '!' cannot be applied to operand of type '" +
                    exp.ResultType.FullName + "'");
            }
        }

        /// <summary>
        /// Generates the IL for the expression using the provided <see cref="ILGenerator"/>.
        /// </summary>
        /// <param name="gen">The generator to emit the code with.</param>
        public override void Generate(ILGenerator gen)
        {
            m_exp.Generate(gen);
            gen.Emit(OpCodes.Ldc_I4_0);
            gen.Emit(OpCodes.Ceq);
        }

        /// <summary>
        /// Generates the required branch for a loop.
        /// </summary>
        /// <param name="gen">The generator to emit the code with.</param>
        /// <param name="label">The label to branch to given the branch case.</param>
        /// <param name="branchCase">The branch condition.</param>
        public override void GenerateForBranch(ILGenerator gen, Label label, bool branchCase)
        {
            if (m_exp is CodeConditionExpression)
            {
                ((CodeConditionExpression)m_exp).GenerateForBranch(gen, label, !branchCase);
            }
            else
            {
                m_exp.Generate(gen);
                if (branchCase)
                    gen.Emit(OpCodes.Brfalse, label);
                else
                    gen.Emit(OpCodes.Brtrue, label);
            }
        }

        /// <summary>
        /// Prints the expression to a <see cref="CodeWriter"/>.
        /// </summary>
        /// <param name="cp">The target <see cref="CodeWriter"/> to print the code to.</param>
        public override void PrintCode(CodeWriter cp)
        {
            cp.Write("!");
            m_exp.PrintCode(cp);
        }

        /// <summary>
        /// Gets the result type for this expression.
        /// </summary>
        /// <remarks>The value will always be a boolean type.</remarks>
        public override Type ResultType
        {
            get { return typeof(bool); }
        }
    }
}
